/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Interface.Patrocinio;

import static Interface.Global.aux;
import Modelo.Edicao.Edicao;
import Modelo.Evento.Evento;
import Modelo.Patrocinador.Patrocinador;
import Modelo.Patrocinio.Patrocinio;
import Util.ExcecaoInstanciaNaoEncontrada;
import java.awt.Component;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author ana.siena
 */
public class ConsultarPatrocinio extends javax.swing.JFrame {

    private Long codEven;
    private Long nroEdi;
    private String CNPJPatroc;
    private float valorPatrocinio;
    private float saldoPatrocinio;
    private Patrocinio patroci;
            
    public ConsultarPatrocinio() {
        initComponents();
        init();
    }
    
    private void init(){
	setVisible(true);
        setTitle("Consultar Patrocinio");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                
        //Preenche combobox do evento
        ArrayList <Evento> vetorCodEvento = new ArrayList <Evento>();
        vetorCodEvento = (ArrayList<Evento>) aux.buscarTodosEventos();
        
        for(int i=0; i<vetorCodEvento.size();i++)
        {
            codEventoCombo.addItem(vetorCodEvento.get(i).getCodEvento());
        }
        
        ArrayList <Patrocinador> vetorCNPJ = new ArrayList <Patrocinador>();
        vetorCNPJ = (ArrayList<Patrocinador>) aux.buscarTodosPatrocinadores();
              
        CNPJPatrocionio.removeAllItems();
        for(int i=0; i<vetorCNPJ.size();i++)
        {   
            CNPJPatrocionio.addItem(vetorCNPJ.get(i).getCnpjPatrocinador());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        codEventoCombo = new javax.swing.JComboBox();
        nroEdicaoCombo = new javax.swing.JComboBox();
        CNPJPatrocionio = new javax.swing.JComboBox();
        botaoConsultar = new javax.swing.JButton();
        botaoRetornar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        valorPatroc = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        saldoPatroc = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Consultar um Patrocinio");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Entradas"));

        jLabel2.setText("Codigo do Evento:");

        jLabel3.setText("Nro da Edicao:");

        jLabel4.setText("CNPJ do Patrocinador:");

        codEventoCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codEventoComboActionPerformed(evt);
            }
        });

        nroEdicaoCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nroEdicaoComboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(CNPJPatrocionio, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(38, 38, 38)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(codEventoCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(nroEdicaoCombo, 0, 137, Short.MAX_VALUE))))
                .addContainerGap(94, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(codEventoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nroEdicaoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(CNPJPatrocionio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        botaoConsultar.setText("Consultar");
        botaoConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoConsultarActionPerformed(evt);
            }
        });

        botaoRetornar.setText("Retornar");
        botaoRetornar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoRetornarActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados sobre o patrocinio"));

        valorPatroc.setEnabled(false);
        valorPatroc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valorPatrocActionPerformed(evt);
            }
        });

        jLabel6.setText("Valor do Patrocinio:");

        jLabel7.setText("Saldo do Patrocinio:");

        saldoPatroc.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(valorPatroc, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                    .addComponent(saldoPatroc))
                .addGap(94, 94, 94))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(valorPatroc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(saldoPatroc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 110, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(114, 114, 114))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(botaoRetornar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botaoConsultar)
                                .addContainerGap())))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoConsultar)
                    .addComponent(botaoRetornar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoConsultarActionPerformed
        codEven = Long.parseLong(""+ codEventoCombo.getSelectedItem());
        nroEdi = Long.parseLong(""+ nroEdicaoCombo.getSelectedItem());
        CNPJPatroc = (""+ CNPJPatrocionio.getSelectedItem());
              
        try {        
            patroci = aux.buscaPatrocinio(CNPJPatroc, codEven, nroEdi);
        } catch (ExcecaoInstanciaNaoEncontrada ex) {
            Logger.getLogger(ConsultarPatrocinio.class.getName()).log(Level.SEVERE, null, ex);
            Component frame = null;
            JOptionPane.showMessageDialog(frame,ex.getMessage());
        }
        
        valorPatroc.setText(Float.toString(patroci.getValorPatrocinio()));
        saldoPatroc.setText(Float.toString(patroci.getSaldoPatrocinio()));   
    }//GEN-LAST:event_botaoConsultarActionPerformed

    private void botaoRetornarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoRetornarActionPerformed
        new MenuPatrocinio().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_botaoRetornarActionPerformed

    private void codEventoComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codEventoComboActionPerformed
        codEven = Long.parseLong(""+ codEventoCombo.getSelectedItem());
               
        if(codEven == 0){
            System.out.println("Nao tem nenhum evento selecionado");
        }
        else{
            //Preenche combobox do edicao
            ArrayList <Edicao> vetorCodEdicao = new ArrayList <Edicao>();
            vetorCodEdicao = (ArrayList<Edicao>) aux.buscaEdicoesPorEvento(codEven);
            
            nroEdicaoCombo.removeAllItems();
            for(int i=0; i<vetorCodEdicao.size();i++)
            {
                nroEdicaoCombo.addItem(vetorCodEdicao.get(i).getNroEdicao());
            }
        }
    }//GEN-LAST:event_codEventoComboActionPerformed

    private void nroEdicaoComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nroEdicaoComboActionPerformed
        
    }//GEN-LAST:event_nroEdicaoComboActionPerformed

    private void valorPatrocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valorPatrocActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_valorPatrocActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox CNPJPatrocionio;
    private javax.swing.JButton botaoConsultar;
    private javax.swing.JButton botaoRetornar;
    private javax.swing.JComboBox codEventoCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox nroEdicaoCombo;
    private javax.swing.JTextField saldoPatroc;
    private javax.swing.JTextField valorPatroc;
    // End of variables declaration//GEN-END:variables
}
